﻿using System.Web.Mvc;
using Nop.Plugin.Widgets.NivoSlider.Models;
using Nop.Services.Configuration;
using Nop.Services.Media;
using Nop.Web.Framework.Controllers;

namespace Nop.Plugin.Widgets.NivoSlider.Controllers
{
    public class WidgetsNivoSliderController : Controller
    {
        private readonly IPictureService _pictureService;
        private readonly NivoSliderSettings _nivoSliderSettings;
        private readonly ISettingService _settingService;

        public WidgetsNivoSliderController(IPictureService pictureService, 
            NivoSliderSettings nivoSliderSettings, ISettingService settingService)
        {
            this._pictureService = pictureService;
            this._nivoSliderSettings = nivoSliderSettings;
            this._settingService = settingService;
        }
        
        [AdminAuthorize]
        [ChildActionOnly]
        public ActionResult Configure()
        {
            var model = new ConfigurationModel
                            {
                                Picture1Id = _nivoSliderSettings.Picture1Id,
                                Text1 = _nivoSliderSettings.Text1,
                                Link1 = _nivoSliderSettings.Link1,
                                Picture2Id = _nivoSliderSettings.Picture2Id,
                                Text2 = _nivoSliderSettings.Text2,
                                Link2 = _nivoSliderSettings.Link2,
                                Picture3Id = _nivoSliderSettings.Picture3Id,
                                Text3 = _nivoSliderSettings.Text3,
                                Link3 = _nivoSliderSettings.Link3,
                                Picture4Id = _nivoSliderSettings.Picture4Id,
                                Text4 = _nivoSliderSettings.Text4,
                                Link4 = _nivoSliderSettings.Link4,
                                Picture5Id = _nivoSliderSettings.Picture4Id,
                                Text5 = _nivoSliderSettings.Text4,
                                Link5 = _nivoSliderSettings.Link4,
                                Picture6Id = _nivoSliderSettings.Picture4Id,
                                Text6 = _nivoSliderSettings.Text4,
                                Link6 = _nivoSliderSettings.Link4
                            };

            return View("Nop.Plugin.Widgets.NivoSlider.Views.WidgetsNivoSlider.Configure", model);
        }

        [HttpPost]
        [AdminAuthorize]
        [ChildActionOnly]
        public ActionResult Configure(ConfigurationModel model)
        {
            if (!ModelState.IsValid)
                return Configure();

            //save settings
            _nivoSliderSettings.Picture1Id = model.Picture1Id;
            _nivoSliderSettings.Text1 = model.Text1;
            _nivoSliderSettings.Link1 = model.Link1;

            _nivoSliderSettings.Picture2Id = model.Picture2Id;
            _nivoSliderSettings.Text2 = model.Text2;
            _nivoSliderSettings.Link2 = model.Link2;

            _nivoSliderSettings.Picture3Id = model.Picture3Id;
            _nivoSliderSettings.Text3 = model.Text3;
            _nivoSliderSettings.Link3 = model.Link3;

            _nivoSliderSettings.Picture4Id = model.Picture4Id;
            _nivoSliderSettings.Text4 = model.Text4;
            _nivoSliderSettings.Link4 = model.Link4;

            _nivoSliderSettings.Picture5Id = model.Picture5Id;
            _nivoSliderSettings.Text5 = model.Text5;
            _nivoSliderSettings.Link5 = model.Link5;

            _nivoSliderSettings.Picture6Id = model.Picture6Id;
            _nivoSliderSettings.Text6 = model.Text6;
            _nivoSliderSettings.Link6 = model.Link6;
            _settingService.SaveSetting(_nivoSliderSettings);

            return Configure();
        }

        [ChildActionOnly]
        public ActionResult PublicInfo(string widgetZone)
        {
            var model = new PublicInfoModel
                            {
                                Picture1Url =
                                    _pictureService.GetPictureUrl(_nivoSliderSettings.Picture1Id,
                                                                  showDefaultPicture: false),
                                Text1 = _nivoSliderSettings.Text1,
                                Link1 = _nivoSliderSettings.Link1,
                                Picture2Url =
                                    _pictureService.GetPictureUrl(_nivoSliderSettings.Picture2Id,
                                                                  showDefaultPicture: false),
                                Text2 = _nivoSliderSettings.Text2,
                                Link2 = _nivoSliderSettings.Link2,
                                Picture3Url =
                                    _pictureService.GetPictureUrl(_nivoSliderSettings.Picture3Id,
                                                                  showDefaultPicture: false),
                                Text3 = _nivoSliderSettings.Text3,
                                Link3 = _nivoSliderSettings.Link3,
                                Picture4Url =
                                    _pictureService.GetPictureUrl(_nivoSliderSettings.Picture4Id,
                                                                  showDefaultPicture: false),
                                Text4 = _nivoSliderSettings.Text4,
                                Link4 = _nivoSliderSettings.Link4,
                                Picture5Url =
                                    _pictureService.GetPictureUrl(_nivoSliderSettings.Picture5Id,
                                                                  showDefaultPicture: false),
                                Text5 = _nivoSliderSettings.Text5,
                                Link5 = _nivoSliderSettings.Link5,
                                Picture6Url =
                                    _pictureService.GetPictureUrl(_nivoSliderSettings.Picture6Id,
                                                                  showDefaultPicture: false),
                                Text6 = _nivoSliderSettings.Text6,
                                Link6 = _nivoSliderSettings.Link6
                            };

            if (string.IsNullOrEmpty(model.Picture1Url) && string.IsNullOrEmpty(model.Picture2Url) &&
                string.IsNullOrEmpty(model.Picture3Url) && string.IsNullOrEmpty(model.Picture4Url))
            {
                //no pictures uploaded
                return Content("");
            }

            return View("Nop.Plugin.Widgets.NivoSlider.Views.WidgetsNivoSlider.PublicInfo", model);
        }
    }
}